﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChineseChessFen
{
    /// <summary>
    /// Fen格式与二维数组互转
    /// </summary>
    public static class FenUtil
    {
        /// <summary>
        /// 10行
        /// </summary>
        private const int row = 10;
        /// <summary>
        /// 9列
        /// </summary>
        private const int col = 9;

        /// <summary>
        /// 判断棋子颜色
        /// </summary>
        /// <param name="piece"></param>
        /// <returns></returns>
        private static PieceColor GetPieceColor(char piece)
        {
            if (piece >= 'a' && piece <= 'z')
            {
                return PieceColor.Black;
            }
            else if (piece >= 'A' && piece <= 'Z')
            {
                return PieceColor.Red;
            }
            else
            {
                return PieceColor.White;
            }
        }

        /// <summary>
        /// 局面转Fen
        /// </summary>
        /// <param name="board"></param>
        /// <returns></returns>
        public static string BoardToFen(char[,] board)
        {
            List<string> result = new();
            for(int i = 0; i < row; i++)
            {
                int count = 0;
                string s = "";
                for(int j = 0; j < col; j++)
                {
                    if (GetPieceColor(board[i, j]) == PieceColor.White)
                    {
                        count++;
                    }
                    else
                    {
                        if (count > 0)
                        {
                            s += count;
                            count = 0;
                        }
                        s += board[i, j];
                    }
                }
                if (count > 0)
                {
                    s += count;
                }
                result.Add(s);
            }
            return string.Join('/', result);
        }

        /// <summary>
        /// Fen转局面
        /// </summary>
        /// <param name="fen"></param>
        /// <returns></returns>
        public static char[,] FenToBoard(string fen)
        {
            char[,] board = new char[row, col];
            for(int i = 0; i < row; i++)
            {
                for(int j = 0; j < col; j++)
                {
                    board[i, j] = '0';
                }
            }

            string[] rs = fen.Split('/');
            for(int i = 0; i < row; i++)
            {
                int k = 0;
                for(int j = 0; j < rs[i].Length; j++,k++)
                {
                    if (rs[i][j] >= '1' && rs[i][j] <= '9')
                    {
                        k += (rs[i][j] - '0' - 1);
                    }
                    else
                    {
                        board[i, k] = rs[i][j];
                    }
                }
            }
            return board;
        }

        /// <summary>
        /// 棋子颜色
        /// </summary>
        private enum PieceColor
        {
            /// <summary>
            /// 空
            /// </summary>
            White,
            /// <summary>
            /// 红
            /// </summary>
            Red,
            /// <summary>
            /// 黑
            /// </summary>
            Black
        }

    }
}
